<?php

// +----------------------------------------------------------------------
// | Date: 2018-05-04
// +----------------------------------------------------------------------
// | Author: zj 
// +----------------------------------------------------------------------
// | 游戏配置管理
// +----------------------------------------------------------------------

namespace app\game\controller;

use cmf\controller\UserBaseController;

class GameconfigController extends UserBaseController{

    public function index() {
        $p = $this->request->param('p') ?: 1;
        $pageSize = 10;
        $sys = new SystemController();
        //游戏配置
        $query = 'totalQuery';
        $game_config = 'game_config';
        $mobile_version = 'mobile_version';
        $config_data = array();
        $config_data['model'] = $game_config;
        $config_data['field'] = 'game_id,game_key_c,game_m,run_args_x,least_q,maxinum_q,config_i,enable_i,order_n,hidden_pc';
        $config_data['order'] = 'game_id';
        $config_data['limit'] = ($p - 1) * $pageSize . ',' . $pageSize;
        $configData = $sys->getDbMes($query, $config_data);
        //总条数和数据集
        $total = $configData['totalCount'];
        $result = $configData['result'];
        //版本控制
        $gameKeys = array_column($result, 'game_key_c');
        $version_data['model'] = $mobile_version;
        $version_data['field'] = 'game_key,os_x,hidden_i';
        $version_data['where'] = ['game_key' => ['in', $gameKeys]];
        $versionData = $sys->getDbMes($query, $version_data);
        $verData = $versionData['result'];
        foreach ($result as $k => $v) {
            foreach ($verData as $s) {
                if ($v['game_key_c'] == $s['game_key']) {
                    if ($s['os_x'] == 'i') {
                        $result[$k]['hidden_i'] = $s['hidden_i'] == 1 ? 1 : 0;
                    } elseif ($s['os_x'] == 'a') {
                        $result[$k]['hidden_a'] = $s['hidden_i'] == 1 ? 1 : 0;
                    }
                    unset($s);
                }
            }
        }
        $pdata = ['list' => $result, 'total' => $total];
        $this->result($pdata, 1, '请求成功', 'json', [], 'status');
    }
    //添加游戏配置
    public function addGameConfig() {
        $pdata = $this->request->param();
        $gameId = $this->request->param('game_id');
        $gameKey = $this->request->param('game_key_c');
        $configI = $this->request->param('config_i');
        $enableI = $this->request->param('enable_i');
        $hiddenPC = $this->request->param('hidden_pc');
        //游戏配置
        $query = 'totalQuery';
        $add = 'totalAdd';
        $game_config = 'game_config';
        $mobile_version = 'mobile_version';
        $sys = new SystemController();
        //game_id和game_key是否是唯一
        $hasRep = array();
        $hasRep['model'] = $game_config;
        $hasRep['where'] = ['game_id' => $gameId];
        $hasRep['query'] = 'find';
        $hasId = $sys->getDbMes($query, $hasRep);
        $hasRep['where'] = ['gaem_key_c' => $gameKey];
        $hasKey = $sys->getDbMes($query, $hasRep);
        $uniqueId = $hasId['result'];
        $uniqueKey = $hasKey['result'];
        if (empty($uniqueId)) {
            $this->result([], 0, 'game_id已经存在了', 'json', [], 'status');
        }
        if (empty($uniqueKey)) {
            $this->result([], 0, 'game_key_c已经存在了', 'json', [], 'status');
        }

        $pdata['config_i'] = !empty($configI) ? 1 : 0;
        $pdata['enable_i'] = !empty($enableI) ? 1 : 0;
        $pdata['hidden_pc'] = !is_null($hiddenPC) ? 0 : 1;

        // 插入
        $addRep['model'] = $game_config;
        $addRep['data'] = $pdata;
        $data = $sys->alterDbMes($add, $addRep);
        if ($data == 'no') {
            $this->result([], 0, '添加失败', 'json', [], 'status');
        }
        $isAhidden = $this->request->param('hidden_a');
        $isIhidden = $this->request->param('hidden_i');
        $hiddenAndroid = is_null($isAhidden) ? 1 : 0;
        $hiddenIos = is_null($isIhidden) ? 1 : 0;

        $addData = array();
        $addData['bundle_id'] = 'com.youxia.' . $gameKey;
        $addData['bundle_version_x'] = '1.0.0';
        $addData['game_name'] = $this->request->param('game_m');
        $addData['game_key'] = $gameKey;
        $addData['force_upgrade_i'] = 0;
        $addData['icon_url'] = '';
        //添加a端
        $addData['os_x'] = 'a';
        $addData['upload_path_x'] = 'http://cdn.3138.com/games/a/';
        $addData['hidden_i'] = $hiddenAndroid;
        $addRep = array();
        $addRep['model'] = $mobile_version;
        $addRep['data'] = $addData;
        $flago = $sys->alterDbMes($add, $addRep);

        //添加i端
        $addData['os_x'] = 'i';
        $addData['upload_path_x'] = '';
        $addData['hidden_i'] = $hiddenIos;
        $addRep['model'] = $mobile_version;
        $addRep['data'] = $addData;
        $flagt = $sys->alterDbMes($add, $addRep);
        if ($flago == 'ok' && $flagt == 'ok') {
            $this->result([], 1, '添加成功', 'json', [], 'status');
        } else {
            $this->result([], 0, '添加失败', 'json', [], 'status');
        }
    }

    //修改第一步
    public function modifyFirstStep() {
        $oldGameId = $this->request->param('old_game_id');
        $oldGameKey = $this->request->param('old_game_key_c');
        $gameId = $this->request->param('game_id');
        $gameKey = $this->request->param('game_key_c');
        if(empty($oldGameId) || empty($oldGameKey)){
            $this->result([], 0, '参数错误', 'json', [], 'status');
        }
        if ($oldGameId == $gameId && $oldGameKey == $gameKey) {
            $this->result([], 0, '没有修改无需保存', 'json', [], 'status');
        }
        $query = 'totalQuery';
        $alter = 'totalAlter';
        $sys = new SystemController();
        //game_id和game_key是否是唯一
        $hasRep = array();
        $hasRep['model'] = 'game_config';
        $hasRep['where'] = ['game_id' => $gameId];
        $hasRep['query'] = 'find';
        $hasId = $sys->getDbMes($query, $hasRep);
        $hasRep['where'] = ['gaem_key_c' => $gameKey];
        $hasKey = $sys->getDbMes($query, $hasRep);
        $uniqueId = $hasId['result'];
        $uniqueKey = $hasKey['result'];
        $alterRep['model'] = 'game_config';
        $alterRep['where'] = ['game_id'=>$oldGameId,'game_key_c'=>$oldGameKey];
        $alterRep['data']['game_id'] = empty($uniqueId)?$gameId:$oldGameId;
        $alterRep['data']['game_key_c'] = empty($uniqueKey)?$gameKey:$oldGameKey;
        $data = $sys->alterDbMes($alter, $alterRep);
        if($data=='ok'){
            $this->result([], 1, '修改成功', 'json', [], 'status');
        }else{
            $this->result([], 0, '修改失败', 'json', [], 'status');
        }
        
    }
    //修改第二步
    public function modifySecondStep() {
        $pdata = $this->request->param();
        $oldGameId = $this->request->param('old_game_id');
        $oldGameKey = $this->request->param('old_game_key_c');
        $configI = $this->request->param('config_i');
        $enableI = $this->request->param('enable_i');
        $hiddenPC = $this->request->param('hidden_pc');
        if(empty($oldGameId) || empty($oldGameKey)){
            $this->result([], 0, '参数错误', 'json', [], 'status');
        }
        $pdata['config_i'] = !empty($configI) ? 1 : 0;
        $pdata['enable_i'] = !empty($enableI) ? 1 : 0;
        $pdata['hidden_pc'] = !is_null($hiddenPC) ? 0 : 1;
        $alter = 'totalAlter';
        $sys = new SystemController();
        $addRep['model'] = 'game_config';
        $addRep['where'] = ['game_key_c'=>$oldGameKey,'game_id'=>$oldGameId];
        $addRep['data'] = $pdata;
        $config = $sys->alterDbMes($alter, $addRep);
        
        //根据game key 在修改移动版中的显示参数
        $isAhidden = $this->request->param('hidden_a');
        $isIhidden = $this->request->param('hidden_i');
        $hiddenAndroid = is_null($isAhidden) ? 1 : 0;
        $hiddenIos = is_null($isIhidden) ? 1 : 0;
        //改变状态
        $alterRep['model'] = 'mobile_version';
        $alterRep['where'] = ['game_key'=>$oldGameKey,'os_x'=>'i'];
        $alterRep['data'] = ['hidden_i'=>$hiddenIos];
        $mobilei = $sys->alterDbMes($alter, $alterRep);
        $alterRep['data'] = ['hidden_i'=>$hiddenAndroid];
        $alterRep['where'] = ['game_key'=>$oldGameKey,'os_x'=>'a'];
        $mobilea = $sys->alterDbMes($alter, $alterRep);
        if($config == 'ok' || $mobilei=='ok' ||  $mobilea=='ok'){
            $this->result([], 1, '修改成功', 'json', [], 'status');
        }else{
            $this->result([], 0, '修改失败', 'json', [], 'status');
        }
    }
    //获取游戏配置详情
    public function getConfigDetail() {
        $gamekey = $this->request->param('gamekey');
        if (empty($gamekey)) {
            $this->result([], 0, 'gamekey不能为空', 'json', [], 'status');
        }
        $sys = new SystemController();
        $query = 'totalQuery';
        $game_config = 'game_config';
        $mobile_version = 'mobile_version';
        //游戏配置
        $config_data['model'] = $game_config;
        $config_data['query'] = 'find';
        $config_data['field'] = 'game_id,game_key_c,game_m,run_args_x,least_q,maxinum_q,config_i,enable_i,order_n,hidden_pc';
        $config_data['where'] = ['game_key_c' => $gamekey];
        $configData = $sys->getDbMes($query, $config_data);
        $data = $configData['result'];
        //版本
        if (!empty($data)) {
            $version_data['query'] = 'find';
            $version_data['model'] = $mobile_version;
            $version_data['field'] = 'game_key,os_x,hidden_i';
            $version_data['where'] = ['game_key' => $gamekey, 'os_x' => 'a'];
            $versionData1 = $sys->getDbMes($query, $version_data);
            $version_data['where'] = ['game_key' => $gamekey, 'os_x' => 'i'];
            $versionData2 = $sys->getDbMes($query, $version_data);
            $data ['hidden_a'] = empty($versionData1['result']) ? 1 : $versionData1['result']['hidden_i'];
            $data ['hidden_i'] = empty($versionData2['result']) ? 1 : $versionData2['result']['hidden_i'];
            $this->result($data, 1, '请求成功', 'json', [], 'status');
        }else{
            $this->result($data, 0, '请求失败', 'json', [], 'status');
        }
        
    }

    public function delete() {
        $gamekey = $this->request->param('gamekey');
        if (empty($gamekey)) {
            $this->result([], 0, 'gamekey不能为空', 'json', [], 'status');
        }
        $sys = new SystemController();
        $del = 'totalDel';
        $game_config = 'game_config';
        $mobile_version = 'mobile_version';
        //游戏配置
        $config_data['model'] = $game_config;
        $config_data['where'] = ['game_key_c' => $gamekey];
        $configData = $sys->getDbMes($del,$config_data );
        if ($configData == 'ok') {
            $ver_data['model'] = $mobile_version;
            $ver_data['where'] = ['game_key' => $gamekey];
            $sys->getDbMes($del, $ver_data);
            $this->result([], 1, '删除成功', 'json', [], 'status');
        } else {
            $this->result([], 0, '删除失败', 'json', [], 'status');
        }
    }

}
